package com.gfxy.educational.controller;

import com.gfxy.commons.message.Message;
import com.gfxy.commons.message.MessageUtil;
import com.gfxy.commons.utils.UUIDUtil;
import com.gfxy.educational.entity.EduGrade;
import com.gfxy.educational.service.GradeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author: lijixin 2019-06-04
 */
@Controller
@RequestMapping("/educational/grade")
public class GradeController {

    private static final Logger logger = LoggerFactory.getLogger(GradeController.class);

    @Autowired
    private GradeService gradeService;

    @RequestMapping("/index.action")
    public String index(){
        return "educational/grade";
    }


    @RequestMapping("/getGradeAll")
    public @ResponseBody List<EduGrade> getGradeAll(){
        logger.info("查询所有的年级信息");
        return gradeService.getAllGrade();
    }

    @RequestMapping("/saveGrade")
    public @ResponseBody
    Message saveGradeInfo(HttpServletRequest request){
        String name = request.getParameter("name");
        String state = request.getParameter("state");
        String gradeId = request.getParameter("gradeId");
        if(gradeId == null || gradeId.isEmpty()){
            //新增
            EduGrade grade = new EduGrade();
            grade.setGradeId(UUIDUtil.getUUID());
            grade.setGradeName(name);
            grade.setState(Integer.parseInt(state));
            int i = gradeService.saveGrade(grade);
            return MessageUtil.getSuccessMsg(i);
        } else {
            EduGrade grade = new EduGrade();
            grade.setGradeId(gradeId);
            grade.setGradeName(name);
            grade.setState(Integer.parseInt(state));
            int i = gradeService.updateGrade(grade);
            return MessageUtil.getSuccessMsg(i);
        }

    }

    @RequestMapping("/deleteGrade")
    public @ResponseBody Message deleteGradeInfo(HttpServletRequest request){
        String gradeId = request.getParameter("gradeId");

        logger.info("删除年级：[" + gradeId + "]");
        int result = gradeService.deleteGrade(gradeId);
        return MessageUtil.getSuccessMsg(result);
    }
}
